package com.edee.bigday.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.edee.bigday.order.dao.OrderItemDao;
import com.edee.bigday.order.entity.OrderEntity;
import com.edee.bigday.order.entity.OrderItemEntity;
import com.edee.bigday.order.entity.OrderReturnReasonEntity;
import com.edee.bigday.order.service.OrderItemService;
import com.edee.common.utils.PageUtils;
import com.edee.common.utils.Query;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@RabbitListener(queues = {"edee-java-queue"})
@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues：声明需要监听的所有队列
     * 消息的类型为：class org.springframework.amqp.core.Message
     * 下面recieveMessage()方法的参数可以写为：
     * 1、Message类型（原生消息详细信息：消息头+消息体）
     * 2、T<发送消息的类型>：如OrderReturnReasonEntity；
     * 3、Channel（com.rabbitmq.client.Channel，当前传输数据的通道）
     * Queue：可以被多方监听，但只能有一方收到消息，消息被接收后即被从队列中删除。
     * 模拟场景：
     * 1）启动多个订单服务，同时监听同一消息队列，多个订单服务会同时收到消息吗？
     * 同一条消息，只能被一个客户端接收到。
     * 2）一条消息处理完成后，才能接收下一条消息；
     */
    //@RabbitListener(queues = {"edee-java-queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity content, Channel channel) throws InterruptedException {
        System.out.println("接收到信息：" + content);
        byte[] body = message.getBody();
        //body结果为：{"id":10,"name":"Deal with quality problems！","sort":null,"status":2,"createTime":null}
        //消息头属性信息为：
        MessageProperties properties = message.getMessageProperties();
        //System.out.println("接收到信息，内容为："+message+"===》类型为："+message.getClass());
        //System.out.println("接收到信息："+message+"===》内容为："+content);
        //Thread.sleep(3000);
        System.out.println("消息处理完成：" + content.getName());
        //下面的deliveryTag是在Channel内自增的：
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        //签收消息：multiple:false--非批量签收，即逐条签收

        try {
            if (deliveryTag % 2 == 0) {
                //签收
                channel.basicAck(deliveryTag, false);
                System.out.println("消息已签收。。。");
            } else {
                //拒收：
                //void basicNack(long deliveryTag, boolean multiple, boolean requeue(重新入队))
                //requeue=false:将消息丢弃，requeue=true:将消息发回服务器，消息重新入队：
                //channel.basicNack(deliveryTag,false,true);
                channel.basicNack(deliveryTag,false,false);
                //void basicReject(long deliveryTag, boolean requeue) throws IOException;
                //channel.basicReject(deliveryTag,false);
                System.out.println("消息未签收！！！");
            }
        } catch (IOException e) {
            //网络中断等意外情况：
            e.printStackTrace();
        }
    }

    @RabbitHandler
    public void recieveMessage2(OrderEntity content) {
        System.out.println("002-接收到消息：" + content);
    }
}